var $, element, form, layedit, dtree, transfer, table, upload, util;

setTimeout(function() {

    if (rightControl != undefined) {
        layui.extend({
            dtree: '../static/lib/layui_ext/dtree/dtree' // {/}的意思即代表采用自有路径，即不跟随 base 路径
        }).use(['dtree', 'element', 'jquery', 'form', 'layedit', 'table', 'upload', 'transfer', 'util'], function() {
            $ = layui.jquery,
                element = layui.element,
                form = layui.form,
                layedit = layui.layedit,
                dtree = layui.dtree,
                transfer = layui.transfer,
                table = layui.table,
                upload = layui.upload,
                util = layui.util;

            console.log(rightControl);

            //初始化表格 表头
            AJAX(oveUrl + "/layuiTabCols/seleElem", {
                    elem: '#' + rightControl.tabElemId
                }, 'GET', null, null,
                (r) => {
                    initCols(r)
                },
                (r) => {

                });

            function getTreeFun() {
                //初始化树
                AJAX(oveUrl + "/" + rightControl.treeLeft, null, 'GET', null, null,
                    (r) => {
                        initTree(r.data)
                    },
                    (r) => {

                    });
            }


            /*
             * 初始化表格 表头
             */
            function initCols(cols) {
                console.log(cols)
                tab = table.render({
                    id: rightControl.tabElemId,
                    elem: '#' + rightControl.tabElemId,
                    url: oveUrl + "/" + rightControl.oveTabUrl + "/getTab",
                    toolbar: '#toolbarTab' //开启头部工具栏，并为其绑定左侧模板
                        ,
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full-30',
                    cellMinWidth: 80,
                    page: true,
                    limit: 10,
                    response: {
                        statusCode: 200 //规定成功的状态码，默认：0
                    },
                    title: '制单',
                    cols: [cols.data]
                });
                //关闭
                layer.close(tabLoad)
            }

            /*
             * 初始化树
             */
            function initTree(TreeData) {
                console.log("初始化树")
                Tree = dtree.render({
                    elem: "#mateTree",
                    id: "mateTree",
                    // elem: "#" + rightControl.treeElem,
                    // id: rightControl.treeElem,
                    method: hymethod,
                    menubar: true,
                    data: TreeData,
                    dataStyle: hydataStyle, //使用layui风格的数据格式
                    dataFormat: hydataFormat, //配置data的风格为list
                    // menubar: true, //开启菜单栏
                    response: {
                        message: "msg",
                        title: rightControl.TreeName,
                        statusCode: 200
                    }, //修改response中返回数据的定义
                    // checkbar: true //开启复选框
                    selectInputName: {
                        nodeId: sysTreeParId,
                        context: sysTreeParName
                    },
                    select: true,
                    selectCardHeight: hyHeight
                });
            }

            //初始化树 向左边获取数据
            if (window.parent.left.TreeDom.data.length != 0) {
                initTree(window.parent.left.TreeDom.data);
            }






            /**                        表头工具栏事件                             */
            table.on('toolbar(' + rightControl.tabElemId + ')', function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                // reInputCsCssSty()
                switch (obj.event) {
                    case 'batAdd':
                        popBadd = createPopenFun(null,
                            1,
                            null,
                            $("#AomPopDiv"),
                            null, null, null, null,
                            null, null, null, ['90%', '210px'],
                            null, null, true, null,
                            function() {

                            },
                            function() {
                                console.log("popBadd销毁")
                            });

                        break;
                    case 'filtrate':
                        //页面层
                        layer.open({
                            title: "筛选",
                            type: 1,
                            area: ['40%', '100%'], //宽高
                            btn: ['查询', '重置'],
                            btnAlign: 'c',
                            yes: function(index, layero) {
                                $("#butFil").click();
                            },
                            btn2: function(index, layero) {
                                depT.reload();
                                $("#butFilres").click();
                                return false;
                            },
                            offset: 'r',
                            anim: 4,
                            shadeClose: true,
                            content: $('#filtrate')
                        });
                        break
                    case 'reloadTab':
                        tab.reload({
                            url: oveUrl + "/" + rightControl.oveTabUrl + "/getTab",
                            page: {
                                curr: 1 //重新从第 1 页开始
                            },
                            where: null
                        });
                        layer.msg("重置成功");
                        break
                };
            });



            element.on('tab(mps-tab)', function(data) {
                var v = form.val('mpsForm');
                console.log(form.val('mpsForm'));
                if (data.index != 0) {
                    if (v.mpsid == '') {
                        $('[lay-filter="imgForm"]').css("display", "none");
                        $('[lay-filter="sizeForm"]').css("display", "none");
                        $('[lay-filter="matForm"]').css("display", "none");
                        $('[lay-filter="craftForm"]').css("display", "none");
                        layer.msg("请完成制单后设置数量分配!")
                    } else {
                        $('[lay-filter="imgForm"]').css("display", "");
                        $('[lay-filter="quaForm"]').css("display", "");
                        $('[lay-filter="sizeForm"]').css("display", "");
                        $('[lay-filter="matForm"]').css("display", "");
                        $('[lay-filter="craftForm"]').css("display", "");
                    }
                }

                switch (data.index) {
                    case 0:
                        console.log(data.index); //得到当前Tab的所在下标
                        break;
                    case 1:
                        console.log(data.index); //得到当前Tab的所在下标

                        break;
                    case 2:
                        $('[lay-filter="quaForm"]').css("display", "none");
                        console.log('数量分配'); //得到当前Tab的所在下标
                        console.log(v)
                        console.log(v.mpsid.length)
                        console.log(v.sizeName == null)
                        console.log(aPoIs.i)
                        console.log(amoTabObj.size)

                        if (v.mpsid.length != 0 && v.sizeName.length != 0) {
                            amoPopFun();
                        } else {
                            $('[lay-filter="quaForm"]').css("display", "none");
                        }

                        break;
                    case 3:
                        console.log(data.index); //得到当前Tab的所在下标

                        break;
                    case 4:
                        console.log(data.index); //得到当前Tab的所在下标

                        break;
                    case 5:
                        console.log(data.index); //得到当前Tab的所在下标

                        break;
                    case 6:
                        console.log(data.index); //得到当前Tab的所在下标

                        break;
                    default:
                        break;
                }

            });



            // 类型
            var AomPopDiv = {
                addTree: function() {
                    console.log(this)
                    popBaddForm =
                        createPopenFun(null,
                            1, null, $('#popTypeForm'), ['提交', '重置', '类型重置'],
                            function() { // 按钮一
                                popTypeFormBtn1(rightControl);
                            },
                            function() { //按钮2
                                popTypeFormBtn2(rightControl);
                            },
                            function() { //按钮3
                                popTypeFormBtn3(rightControl);
                            },
                            null,
                            null,
                            null, ['90%', '45%'],
                            null, null, true,
                            function() {
                                $('.layui-layer-content').css("height", "80%");
                            },
                            function() {
                                popTypeFormSuccessTypeForm(rightControl);
                            },
                            function() {
                                console.log("popBaddForm销毁")
                            });

                },
                edidTree: function() {
                    console.log(this)
                },
                redTree: function() {
                    console.log(this)
                }
            };

            $('#AomPopDiv .layui-btn').on('click', function() {
                layer.close(popBadd);
                var type = $(this).attr('type');
                AomPopDiv[type] ? AomPopDiv[type].call(this) : '';
            });









        })


        //获取左边点击的制单编号
        function getid(obj) {
            console.log(obj);
            var where;
            if (obj.leaf) { //判断点击是是否 是父级
                //不是父级  为 true
                /**
                 *  context: "MPS1911-0003",
                    nodeId: "4",
                    parentId: "-1",
                 */
                where = {
                    mpsid: obj.context,
                    id: obj.nodeId,
                    parentId: obj.parentId
                }

            } else { //如果是父类就查询下级数据
                where = {
                    // mpsid: obj.context,
                    // id: obj.nodeId,
                    parentId: obj.nodeId
                }
            }

            table.reload(rightControl.tabElemId, {
                url: oveUrl + "/" + rightControl.oveTabUrl + "/getInilTab",
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: where,
                done: function(res, curr, count) {
                    console.log(res);
                    layer.msg(res.msg);
                }
            })


        }
    } else {
        layer.msg("必要参数没有记载请重新点击");
        location.reload();
    }

}, 1000);